package net.wanho.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import net.wanho.client.UserClient;
import net.wanho.entity.User;
import net.wanho.util.ExceptionUtil;
import net.wanho.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Author：汤小洋
 * Date：2023-11-09 11:19
 * Description：<描述>
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private UserClient userClient;

    @RequestMapping("/test")
    public String test(){
        // 调用服务提供者的接口
        // String str = restTemplate.getForObject("http://localhost:6001/users/test", String.class);

        String str = userClient.test();

        return "Consumer-->" + str;
    }

    @SentinelResource(value="UserListResource", blockHandlerClass = ExceptionUtil.class, blockHandler = "handlerException")
    @GetMapping
    public AjaxResult getUserList(String username){
        return userClient.getUserList(username);
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteUser(@PathVariable Integer id){
        return userClient.deleteUser(id);
    }

    @PostMapping
    public AjaxResult add(@RequestBody User user){
        return userClient.addUser(user);
    }

    @PutMapping
    public AjaxResult modify(@RequestBody User user){
        return userClient.modifyUser(user);
    }

    @GetMapping("/query")
    public AjaxResult query(@RequestParam Map<String,Object> params){ // 直接使用Map接收参数时，需要添加@RequestParam注解
    // public AjaxResult query(User user){
    //     Map<String, Object> params = new HashMap<>();
    //     params.put("id", user.getId());
    //     params.put("username", user.getUsername());
    //     params.put("password", user.getPassword());
        return userClient.findUser(params);
    }


}
